package com.boss.thirteen.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.entity.AgencySalaryPayment;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.salary.vo.RuleIndexVO;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.thirteen.service.IThirteenSalaryPaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/thirteenSalaryPaymentController")
public class ThirteenSalaryPaymentController extends BasicinfoBaseController {

    private final static Logger logger = LoggerFactory.getLogger(ThirteenSalaryPaymentController.class);

    @Autowired
    private IThirteenSalaryPaymentService iAgencySalaryPaymentService;
    @Autowired
    private IOperrateLogsService iOperrateLogsService;

    @GetMapping("/queryAgencySalaryPaymentPage")
    public ApiResultDTO queryAgencySalaryPaymentPage(@RequestHeader("X-Token") String tokenid,
                                                     @RequestParam(value = "page") String page,
                                                     @RequestParam(value = "limit") String limit,
                                                     @RequestParam(value = "agencyId") String agencyId) {
        try {
            UserDTO userDTO = getUser(tokenid);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            Map<String, Object> queryParam = new HashMap<>(1);
            queryParam.put("agencyId", agencyId);
            queryParam.put("mof_div_code", userDTO.getProvince());
            queryParam.put("fiscal_year", userDTO.getYear());
            queryParam.put("page", page);
            queryParam.put("limit", limit);
            PageDTO<AgencySalaryPayment> pageDTO = iAgencySalaryPaymentService.queryAgencySalaryPaymentPage(paginationDTO, queryParam,tokenid,userDTO);
            return ApiResultDTO.success(pageDTO,null);
        } catch (AppRuntimeException e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    @GetMapping("/queryAgencyIndexPage")
    public ApiResultDTO queryAgencyIndexPage(@RequestHeader("X-Token") String tokenid,
                                             @RequestParam(value = "offset") String offset,
                                             @RequestParam(value = "limit") String limit,
                                             @RequestParam(value = "agencyId") String agencyId,
                                             @RequestParam(value = "payTypeCode") String payTypeCode,
                                             @RequestParam(value = "proCatCode") String proCatCode,
                                             @RequestParam(value = "proCode") String proCode,
                                             @RequestParam(value = "corBgtDocNoName") String corBgtDocNoName,
                                             @RequestParam(value = "depBgtEcoCode") String depBgtEcoCode,
                                             @RequestParam(value = "payCategoryCode") String payCategoryCode,
                                             @RequestParam(value = "funcCode") String funcCode) {
        try {
            UserDTO userDTO = getUser(tokenid);
            Map<String, Object> queryParam = new HashMap<>(2);
            queryParam.put("offset", offset);
            queryParam.put("limit", limit);
            queryParam.put("mof_div_code", userDTO.getProvince());
            queryParam.put("agencyId", agencyId);
            queryParam.put("payTypeCode", payTypeCode);
            queryParam.put("proCatCode", proCatCode);
            queryParam.put("proCode", proCode);
            queryParam.put("corBgtDocNoName", corBgtDocNoName);
            queryParam.put("depBgtEcoCode", depBgtEcoCode);
            queryParam.put("payCategoryCode", payCategoryCode);
            queryParam.put("funcCode", funcCode);
            PageDTO pageDTO = iAgencySalaryPaymentService.getIndexPage(queryParam,tokenid,userDTO);
            return ApiResultDTO.success(pageDTO,null);
        } catch (AppRuntimeException e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }


    @PostMapping("/queryRuleIndexPage")
    public ApiResultDTO queryRuleIndexPage(@RequestHeader("X-Token") String tokenid,
                                            @RequestBody RuleIndexVO ruleIndexVO) {
        try {
            UserDTO userDTO = getUser(tokenid);
            Map<String, Object> queryParam = new HashMap<>(2);
            queryParam.put("offset", ruleIndexVO.getOffset());
            queryParam.put("limit", ruleIndexVO.getLimit());
            queryParam.put("mof_div_code", userDTO.getProvince());

            //queryParam.put("agencyId", agencyId);
            queryParam.put("payTypeCode", ruleIndexVO.getPayTypeCode());
            queryParam.put("proCatCode", ruleIndexVO.getProCatCode());
            queryParam.put("proCode", ruleIndexVO.getProCode());
            queryParam.put("corBgtDocNoName", ruleIndexVO.getCorBgtDocNoName());
            queryParam.put("depBgtEcoCode", ruleIndexVO.getDepBgtEcoCode());
            queryParam.put("payCategoryCode", ruleIndexVO.getPayCategoryCode());
            queryParam.put("govBgtEcoCode", ruleIndexVO.getGovBgtEcoCode());
            queryParam.put("funcCode", ruleIndexVO.getFuncCode());
            PageDTO pageDTO = iAgencySalaryPaymentService.getIndexRulePage(queryParam,ruleIndexVO.getAgencyIds(),userDTO,tokenid);
            return ApiResultDTO.success(pageDTO,null);
        } catch (AppRuntimeException e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 获取已挂接的数据
     * @param salaryId
     * @param personType
     * @param agencyId
     * @return
     */
    @GetMapping("/getHookUpData")
    public ApiResultDTO getHookUpData(@RequestHeader("X-Token") String tokenid,
                                      @RequestParam("salaryId") String salaryId,
                                      @RequestParam("personType") String personType,
                                      @RequestParam("agencyId") String agencyId) {
        try {
            UserDTO userDTO = getUser(tokenid);
            String year = String.valueOf(userDTO.getYear());
            String province = userDTO.getProvince();
            List<AgencySalaryPayment> list = iAgencySalaryPaymentService.getHookUpData(salaryId, personType, year, province, agencyId,tokenid,userDTO);
            return ApiResultDTO.success(list);
        } catch (AppRuntimeException e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 保存挂接数据
     * @return
     */
    @PostMapping("/saveHookUpData")
    public ApiResultDTO saveHookUpData(@RequestHeader("X-Token") String tokenid,
                                       @RequestBody AgencySalaryPayment agencySalaryPayment) {
        try {
            UserDTO userDTO = getUser(tokenid);

            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("工资管理配置--挂接指标规则--指标挂接");
            operrateLogs.setAgencyId(agencySalaryPayment.getAgencyId());
            iOperrateLogsService.saveOperrateLogs(operrateLogs);

            iAgencySalaryPaymentService.saveHookUpData(agencySalaryPayment, userDTO);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "保存数据异常");
        }
    }

    /**
     * 保存规则挂接数据
     * @return
     */
    @PostMapping("/saveRuleHookUpData")
    public ApiResultDTO saveRuleHookUpData(@RequestHeader("X-Token") String tokenid,
                                       @RequestBody AgencySalaryPayment agencySalaryPayment) {
        try {
            UserDTO userDTO = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("工资管理配置--挂接指标规则--保存规则挂接数据");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);

            //规则保存
            iAgencySalaryPaymentService.saveRuleHookUpData(agencySalaryPayment, userDTO);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "保存数据异常");
        }
    }


    /**
     * 在挂接指标前进行冻结指标查询
     * 查询冻结指标方法
     * @param agencyId
     * @return
     */
    @GetMapping("/getPaymentLock")
    public ApiResultDTO getPaymentLock( @RequestParam("agencyId") String agencyId
                                      ) {
        try {
            List<AgencySalaryPayment> list = iAgencySalaryPaymentService.getPaymentLock(agencyId);
            return ApiResultDTO.success(list);
        } catch (AppRuntimeException e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    @GetMapping("/deleteHookUpDataALL")
    public ApiResultDTO deleteHookUpDataALL(@RequestHeader("X-Token") String tokenid ) {
        try {
            UserDTO userDTO = getUser(tokenid);
            iAgencySalaryPaymentService.deleteHookUpDataALL(userDTO);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return ApiResultDTO.success();
    }
}
